package com.iwhalecloud.bss.kite.cucc.service.attr.handler;

import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalErrorConsts;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalServiceOfferConsts;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;
import com.ztesoft.bss.common.util.ContextUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Map;
import java.util.Objects;

/**
 * @ClassName : LocalEparchyAttrHandler
 * @Description : 装机地址地市属性
 * @Author : xie.zhengjun
 * @Date : 2020/9/22 17:23
 */
public class LocalCityAttrHandler extends AbstractAttrHandler<KiteInst, AttrData> {

    @Override
    public void before(KiteAttr attr, KiteInst inst, String value, AttrData data) {

        if (KiteStringUtils.isEmpty(value)) {
            return;
        }
        // 是否跨域属性
        KiteAttr crossDomainAttr = inst.getAttr("200000134");
        // A端城市
        KiteAttr zCityAttr = inst.getAttr("2_0000109");

        if (Objects.isNull(crossDomainAttr) || Objects.isNull(zCityAttr)) {
            return;
        }
        String zCity = zCityAttr.getValue();
        if (KiteStringUtils.isEmpty(zCity)) {
            return;
        }
        // 当是否跨域属性的属性值为本地时，比较A短地市和Z端地市是否相同
        if (KiteStringUtils.isEqual(crossDomainAttr.getValue(), "1_0000404")) {
            if (!KiteStringUtils.isEqual(value, zCity)) {
                LocalErrorConsts.CITY_NOT_EQUAL.throwOut();
            }
        }
    }

    @Override
    public void after(KiteAttr attr, KiteInst inst, AttrData data) {
        // 【上海光纤】质检页面和签订页面的商品信息编辑中 装机地址相关信息是不可修改的
        Map<String, Object> extParams = ContextUtil.getLoginInfo().getUserInfo().getExtParams();
        if (KiteStringUtils.equalsAny(data.getServiceOfferId(), LocalServiceOfferConsts.SERVICE_OFFER_ORDER_SIGN,
            LocalServiceOfferConsts.SERVICE_OFFER_QC, LocalServiceOfferConsts.SERVICE_OFFER_FOLLOW) || (Objects.nonNull(extParams) && extParams.containsKey("nodeCode"))) {
            attr.setIsEdit(KeyConsts.IFTRUE_F);
        }
        super.after(attr, inst, data);
    }
}
